package com.argrace.smart.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.argrace.smart.ArgraceApplication;
import com.argrace.smart.R;
import com.argrace.smart.activity.QingJingDetailActivity;
import com.argrace.smart.activity.RuoQi_DeviceTypeListActivity;
import com.argrace.smart.adapter.QingJingAdapter;
import com.argrace.smart.adapter.RoomAdapter;
import com.argrace.smart.base.BaseFragment;
import com.argrace.smart.model.LocalDb_QingJingTable;
import com.argrace.smart.model.LocalDb_RoomNameTable;
import com.argrace.smart.ui.EmptyRecycleView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by argrace_dev_1 on 2018/3/1.
 */

public class RoomFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener {
    EmptyRecycleView recyclerView;
    SwipeRefreshLayout refreshLayout;
    private RoomAdapter adapter;
    private ArrayList<LocalDb_RoomNameTable> rooms = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.layout_listwithrefresh, container, false);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        init();
        super.onActivityCreated(savedInstanceState);
    }

    private void init() {
        ArgraceApplication.is_RefreshRoomTable = true;
        setView();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (ArgraceApplication.is_RefreshRoomTable) {
            onRefresh();
            ArgraceApplication.is_RefreshRoomTable = false;
        }
    }

    private void setView() {

        recyclerView = (EmptyRecycleView) getView().findViewById(R.id.recyclerView);
        refreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.refreshLayout);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new RoomAdapter(rooms, getActivity());
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        recyclerView.setAdapter(adapter);
//        refreshLayout.setColorSchemeColors(getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme));//getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme), getResources().getColor(R.color.colorTheme)
        refreshLayout.setOnRefreshListener(this);
        adapter.setListeners(new RoomAdapter.OnAdapterItemClickListener() {
            @Override
            public void click(LocalDb_RoomNameTable bean, int position) {
            }
        });

    }

    @Override
    public void onRefresh() {
        setRefreshing(true);
        rooms.clear();
        rooms.addAll(DataSupport.findAll(LocalDb_RoomNameTable.class));
        adapter.notifyDataSetChanged();
        setRefreshing(false);
    }

    public void setRefreshing(final boolean refreshing) {
        refreshLayout.post(new Runnable() {
            @Override
            public void run() {
                refreshLayout.setRefreshing(refreshing);
            }
        });
    }

}

